%******************************************************************
%** Execute Simulation III for TRMCO                             **
%** Created by Min-Hua Huang, Dec 19, 2011                       **
%******************************************************************
clear; clc; format long; record=ones(1,22)*-999; 
load trialdata3.mat;  load TRM_result3.mat; a=0; b=1; 
for i=1:1:1000
    total=trialdata3(1:n(i),(i-1)*4+1:i*4);
    y=total(:,1); x=total(:,2:4); k=size(x,2);
    xs1=total(:,3)-mean(total(:,3));
    xs2=total(:,4)-mean(total(:,4));
    xs=[xs1 xs2]; nocob=TRM_result3(i,2:4)';
    if  TRM_result3(i,5)>0
        nocos=TRM_result3(i,5);
    else
        nocos=0.1;
    end    
    record(i,1)=i; record(i,2)=mu(i); record(i,3)=sigma(i); 
    record(i,4)=a; record(i,5)=b; record(i,6)=w1(i); 
    record(i,7:10)=[nocob;nocos]'; record(i,22)=w2(i);
    nocoymax=max([ones(n(i),1) xs]*nocob); 
    nocoymin=min([ones(n(i),1) xs]*nocob); 
    record(i,11:12)=[nocoymax nocoymin];
    trmest=plm(y,xs,[nocob;nocos],a,b);
    trmb=trmest(1,1:3)'; trmsig=trmest(1,4);    
    if trmsig~=-999
       trmymax=max([ones(n(i),1) xs]*trmb);
       trmymin=min([ones(n(i),1) xs]*trmb);
       record(i,13:16)=[trmb;trmsig]'; record(i,17:18)=[trmymax trmymin];      
       record(i,19)=trmest(6,3); 
    else
       record(i,13:19)=ones(1,7)*-999;
    end   
    record(i,20)=max(y); record(i,21)=min(y); 
    display(i); 
end
save overall_result3.mat